#!/usr/bin/env python
# -*- coding: latin-1; py-indent-offset:4 -*-
################################################################################
# 
# This file is part of Bfplusplus
#
# Bfplusplus is a graphical interface to the Betfair Betting Exchange
# Copyright (C) 2010 Daniel Rodriguez (aka Daniel Rodriksson)
# Copyright (C) 2011 Sensible Odds Ltd.
#
# You can learn more and contact the author at:
#
#    http://code.google.com/p/bfplusplus/
#
# Bfplusplus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bfplusplus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bfplusplus. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""Subclass of UpdateChecker, which is generated by wxFormBuilder."""

import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub

import MainGui

# Implementing UpdateChecker
class UpdateChecker(MainGui.UpdateChecker):
    def __init__(self, parent, version):
	MainGui.UpdateChecker.__init__(self, parent)
        self.myparent = parent

        self.version = version
        self.topic = 'update.version'
	pub.subscribe(self.OnUpdate, self.topic)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.value = 0
        self.m_gaugeUpdateChecking.SetRange(20)
        self.m_gaugeUpdateChecking.SetValue(0)
        self.m_staticTextUpdateTimer.SetLabel('%d seconds remaining' % (20 - self.value))
        

    def OnTimer(self, event):
        if self.value == 20:
            return
        self.value += 1
        self.m_gaugeUpdateChecking.SetValue(self.value)

        self.m_staticTextUpdateTimer.SetLabel('%d seconds remaining' % (20 - self.value))

        if self.value == 20:
            msg = 'The update server could not be reached\n\n' \
                  'If you feel the problem reoccurs on a frequent basis\n' \
                  'Please visit the support page and notify the problem:\n\n' \
                  'http://code.google.com/p/bfplusplus'

            self.m_textCtrlUpdateResult.SetValue(msg)
            self.m_buttonReturn.Enable()

        else:
            self.timer.Start(1000, wx.TIMER_ONE_SHOT)


    def OnUpdate(self, version, exception):
        if self.value == 20:
            return

        self.value = 20
        self.m_gaugeUpdateChecking.SetValue(self.value)
        self.m_gaugeUpdateChecking.Refresh()
        self.m_staticTextUpdateTimer.SetLabel('%d seconds remaining' % (20 - self.value))

        if exception:
            msg = 'An error has ocurred:\n\n' \
                  '%s\n\n' \
                  'If you feel this may be a problem ' \
                  'please visit the support page and notify the problem:\n\n' \
                  'http://code.google.com/p/bfplusplus' % str(exception)

            self.m_textCtrlUpdateResult.SetValue(msg)

            self.m_buttonReturn.Enable()
            return

        forcedUpdate = False
        if version[-1] == 'f':
            forcedUpdate = True
            version = version[:-1]

        if version > self.version:
            if forcedUpdate:
                # Forced update (possibly to overcome a bug or change in API)
                msg = 'A compulsory update of Bfplusplus is available:\n\n' \
                      'Bfplusplus %s\n\n' \
                      'You need to visit the support page, download the new version and install it ' \
                      'before you may continue using Bfplusplus\n\n' \
                      'Press "Open browser to download" below or visit the support page yourself:\n\n' \
                      'http://code.google.com/p/bfplusplus' % version

                self.m_textCtrlUpdateResult.SetValue(msg)

                self.m_buttonExit.SetLabel('Exit Bfplusplus')
                # self.m_buttonExit.Enable()
                self.m_buttonOpenBrowser.Enable()
                return
            else:
                # Non-forced update
            
                msg = 'A new version of Bfplusplus is available:\n\n' \
                      'Bfplusplus %s\n\n' \
                      'It is recommended to visit the support page, download the new version and install it\n\n' \
                      'Press "Open browser to download" below or visit the support page yourself:\n\n' \
                      'http://code.google.com/p/bfplusplus' % version

                self.m_textCtrlUpdateResult.SetValue(msg)

                self.m_buttonReturn.Enable()
                self.m_buttonOpenBrowser.Enable()
                return

        else:
            msg = 'You have the latest version of Bfplusplus'
            self.m_textCtrlUpdateResult.SetValue(msg)
            self.m_buttonReturn.Enable()
	

    def OnButtonClickReturn(self, event):
        event.Skip()
        self.EndModal(0)


    def OnButtonClickBrowser(self, event):
        event.Skip()
        retcode = wx.LaunchDefaultBrowser('http://code.google.com/p/bfplusplus')

        if not retcode:

            wx.MessageBox('Opening the browser seems to have failed\n\n' \
                          'Please visit the support page to download:\n\n'
                          'http://code.google.com/p/bfplusplus',
                          'Opening the browser failed')

        self.EndModal(1)


    def OnButtonClickDoExit(self, event):
        event.Skip()
        self.EndModal(1)


    def OnButtonClickDoCheck(self, event):
        self.value = 0
        self.m_gaugeUpdateChecking.SetRange(20)
        self.m_gaugeUpdateChecking.SetValue(0)
        self.myparent.SeekUpdates(topic=self.topic)
        self.timer.Start(1000, wx.TIMER_ONE_SHOT)
        self.m_staticTextUpdateTimer.SetLabel('%d seconds remaining' % (20 - self.value))
